﻿
/* 
 * Since 2008 - 2013
 */
package com.hh.bss.oa.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.base.*;
import com.hh.bss.common.exception.SystemException;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.oa.dao.*;
import com.hh.bss.oa.entity.*;
import com.hh.bss.oa.service.*;
import com.hh.bss.oa.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class OaMeetingRoomTakingCancelController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private OaMeetingRoomTakingCancelService oaMeetingRoomTakingCancelService;
	private OaMeetingRoomTakingService oaMeetingRoomTakingService;
	
	private OaMeetingRoomTakingCancel oaMeetingRoomTakingCancel;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setOaMeetingRoomTakingCancelServiceImpl(OaMeetingRoomTakingCancelService service) {
		this.oaMeetingRoomTakingCancelService = service;
	}
	public void setOaMeetingRoomTakingServiceImpl(OaMeetingRoomTakingService service) {
		this.oaMeetingRoomTakingService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomTakingCancelQuery oaMeetingRoomTakingCancelQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = oaMeetingRoomTakingCancelService.findPage(oaMeetingRoomTakingCancelQuery);
		
		List<OaMeetingRoomTakingCancel> OaMeetingRoomTakingCancellist = (List) page.getResult();
		ListRange<OaMeetingRoomTakingCancel> resultList = new ListRange<OaMeetingRoomTakingCancel>();
		resultList.setRows(OaMeetingRoomTakingCancellist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomTakingCancel oaMeetingRoomTakingCancel) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomTakingCancel.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oaMeetingRoomTakingCancel,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomTakingCancel.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oaMeetingRoomTakingCancel,"createDt",new Date());}catch(Exception ex){}
				}

			//判断取消的日期是否在预定的范围内
			if (!oaMeetingRoomTakingCancelService.isTaskDay(oaMeetingRoomTakingCancel.getTaskId(),oaMeetingRoomTakingCancel.getMeetingDate()))
				 throw new Exception("取消日期非法:"+DateHelper.toString(oaMeetingRoomTakingCancel.getMeetingDate(), DateHelper.DEFAULT_DATE_FORMAT));
			
			if (oaMeetingRoomTakingCancelService.hasCancel(new Integer(0),oaMeetingRoomTakingCancel.getTaskId(),oaMeetingRoomTakingCancel.getMeetingDate()))
				 throw new Exception("存在重复取消日期:"+DateHelper.toString(oaMeetingRoomTakingCancel.getMeetingDate(), DateHelper.DEFAULT_DATE_FORMAT));
			
			
			oaMeetingRoomTakingCancelService.save(oaMeetingRoomTakingCancel);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,OaMeetingRoomTakingCancel oaMeetingRoomTakingCancel) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        OaMeetingRoomTakingCancel oldoaMeetingRoomTakingCancel = (OaMeetingRoomTakingCancel)oaMeetingRoomTakingCancelService.getById(oaMeetingRoomTakingCancel.getId());
                         
			bind(request,oldoaMeetingRoomTakingCancel);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomTakingCancel.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldoaMeetingRoomTakingCancel,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(OaMeetingRoomTakingCancel.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldoaMeetingRoomTakingCancel,"modifiedDt",new Date());}catch(Exception ex){}
				}
			if (!oaMeetingRoomTakingCancelService.isTaskDay(oaMeetingRoomTakingCancel.getTaskId(),oaMeetingRoomTakingCancel.getMeetingDate()))
				 throw new Exception("取消日期非法:"+DateHelper.toString(oaMeetingRoomTakingCancel.getMeetingDate(), DateHelper.DEFAULT_DATE_FORMAT));


			if (oaMeetingRoomTakingCancelService.hasCancel(oldoaMeetingRoomTakingCancel.getId(),oaMeetingRoomTakingCancel.getTaskId(),oaMeetingRoomTakingCancel.getMeetingDate()))
				 throw new Exception("存在重复取消日期:"+DateHelper.toString(oaMeetingRoomTakingCancel.getMeetingDate(), DateHelper.DEFAULT_DATE_FORMAT));
			

			oaMeetingRoomTakingCancelService.update(oldoaMeetingRoomTakingCancel);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			
			
			
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				
				
				List l=oaMeetingRoomTakingCancelService.getEntityDao().getHibernateTemplate().find("from OaMeetingRoomTaking t where t.status='A' and ( (t.periodType=1 and DATE_FORMAT(t.orderDate,'%Y-%m-%d')>=CURDATE()) or  t.periodType=2 or t.periodType=3 or t.periodType=4)");
				
				OaMeetingRoomTakingCancel oldoaMeetingRoomTakingCancel = (OaMeetingRoomTakingCancel)oaMeetingRoomTakingCancelService.getById(id);
				
				OaMeetingRoomTaking task;
				OaMeetingRoomTaking newTask=null;
				
				for(int j=0;l!=null&&j<l.size();j++){
					task=(OaMeetingRoomTaking)l.get(i);
					newTask=new OaMeetingRoomTaking();
					newTask.setRoomId(task.getRoomId());
					newTask.setOrderDate(oldoaMeetingRoomTakingCancel.getMeetingDate());
					newTask.setBeginDt(oldoaMeetingRoomTakingCancel.getMeetingDate());
					newTask.setPeriodType(new Integer(1));
					newTask.setEndDt(DateHelper.toDate(DateHelper.toString(oldoaMeetingRoomTakingCancel.getMeetingDate(),"yyyy-MM-dd")+" 23:59:59","yyyy-MM-dd HH:mm:ss"));
					if (oaMeetingRoomTakingService.isConflict(newTask,task)) throw new SystemException("不能删除取消，存在冲突的预订:["+task.getTitle()+"],开始日期"+DateHelper.toString(task.getOrderDate(), "yyyy-MM-dd")+",开始时间："+DateHelper.toString(task.getBeginDt(), "HH:mm")+",结束时间："+DateHelper.toString(task.getEndDt(), "HH:mm")); 
				}
				
				
				oaMeetingRoomTakingCancelService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
